package controller;

import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import util.UrlUtility;


import model.PublicHolidayDTO;

import bizlogic.PublicHolidayManager;

/**
 * Servlet implementation class DeletePublicHolidayServlet
 */
@WebServlet("/DeletePublicHolidayServlet")
public class DeletePublicHolidayServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DeletePublicHolidayServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doProcess(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doProcess(request,response);
	}
	private void doProcess(HttpServletRequest req,
			HttpServletResponse res) throws ServletException, IOException {
		
		String error = "";boolean valid = true;
		PublicHolidayManager m = new PublicHolidayManager();
		PublicHolidayDTO pubdaydto ;
		
		 String startDate = req.getParameter("startdate");
		 String endDate = req.getParameter("enddate");
	      DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
	      java.util.Date dtst = null;
	      java.util.Date dttend = null;
		try {
			dtst = df.parse(startDate);
			dttend = df.parse(endDate);
		} catch (ParseException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try
	      {
		      java.sql.Date datestart = new java.sql.Date(dtst.getTime());
		      java.sql.Date dateend = new java.sql.Date(dttend.getTime());
	          pubdaydto= m.findpubDay(datestart, dateend);
			  error = m.deletePublicHoliday(pubdaydto);
				if ( !error.trim().equalsIgnoreCase("") ) valid = false;
	      
			}catch ( NumberFormatException e) {
				valid = false;
				error = "holiday.error.invalidnumbercode";
			}
		UrlUtility.redirectPage(res, "LoadPublicHoliday");		
		
		
	}

}
